package cn.sms.org.controller;

import cn.sms.basic.util.AjaxResult;
import cn.sms.basic.util.PageList;
import cn.sms.org.domain.Employee;
import cn.sms.org.query.EmployeeQuery;
import cn.sms.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService iEmployeeService;

    @GetMapping("/{id}")
    public Employee queryById(@PathVariable("id") Long id){
        return iEmployeeService.queryById(id);
    }

    @GetMapping
    public List<Employee> queryAll(){
        return iEmployeeService.queryAll();
    }

    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            iEmployeeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }

    }

    @PutMapping
    public AjaxResult save(@RequestBody Employee employee){
        try {
            if (employee.getId()==null){
                iEmployeeService.add(employee);
            }else {
                iEmployeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }

    @PostMapping("/list")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query){
        return iEmployeeService.queryPage(query);
    }

    @PatchMapping
    public AjaxResult batchRemoveEmployee(@RequestBody List<Long> ids){
        try {
            iEmployeeService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }

}
